package com.baicun.wish.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import com.baicun.wish.entity.College;
import com.baicun.wish.entity.WishRecommend;
import com.baicun.wish.entity.dto.Result;
import com.baicun.wish.service.CollegeService;
import com.baicun.wish.service.WishRecommendService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baicun
 * @since 2020-09-09
 */
@CrossOrigin
@RestController
@RequestMapping("/wish-recommend")
public class WishRecommendController {

    @Autowired
    private WishRecommendService wishRecommendService;

    @Autowired
    private CollegeService collegeService;

    /**
     * //根据recommendConditionForm高考分数（范围）获取所有推荐大学的信息 10条 一分页
     *     //create index index_wishRecommendByScore on wish_recommend(province,exam_type,lowest_score);
     *     //drop index index_wishRecommendByScore on wish_recommend ;
     * @param jsonObject
     * @return
     */
    @PostMapping("/wishRecommendByScore")
    public Result wishRecommendByScore(@RequestBody JSONObject jsonObject){
        Integer currentPage = jsonObject.getInt("currentPage");
        JSONObject recommendConditionForm = jsonObject.getJSONObject("recommendConditionForm");
        Double score = recommendConditionForm.getDouble("score");
        String province = recommendConditionForm.getStr("province");
        String examType = recommendConditionForm.getStr("examType");
        String admitProbability = recommendConditionForm.getStr("admitProbability");

        QueryWrapper<WishRecommend> wishRecommendQueryWrapper = new QueryWrapper<WishRecommend>().eq("province",province).eq("exam_type",examType);
        //处理推荐
        switch (admitProbability){
            case "冲刺":
                wishRecommendQueryWrapper = wishRecommendQueryWrapper.between("lowest_score",score-10.0,score+10.0);
                break;
            case "稳妥":
                wishRecommendQueryWrapper = wishRecommendQueryWrapper.between("lowest_score",score-30.0,score-10.0);
                break;
            case "保底":
                wishRecommendQueryWrapper = wishRecommendQueryWrapper.between("lowest_score",score-50.0,score-30.0);
                break;
            default:
                wishRecommendQueryWrapper = wishRecommendQueryWrapper.between("lowest_score",score-50.0,score+10.0);
                break;
        }
        Page page = new Page(currentPage,10);
        IPage iPage = wishRecommendService.page(page,wishRecommendQueryWrapper.orderByDesc("lowest_score"));


        if (iPage.getRecords().isEmpty()){
            return Result.success(201,"推荐学校列表为空",iPage);
        }else{
            List<College> collegeList = getCollege(iPage);
            return Result.success(MapUtil.builder().put("ipage", iPage).put("collegeList",collegeList).map());
        }
    }

    /**
     *  //根据recommendConditionForm高考排位（范围）获取所有推荐大学的信息 10条 一分页
     *     //create index index_wishRecommendByRank on wish_recommend(province,exam_type,lowest_rank);
     * @param jsonObject
     * @return
     */
    @PostMapping("/wishRecommendByRank")
    public Result wishRecommendByRank(@RequestBody JSONObject jsonObject){
        Integer currentPage = jsonObject.getInt("currentPage");
        JSONObject recommendConditionForm = jsonObject.getJSONObject("recommendConditionForm");
        Double rank = recommendConditionForm.getDouble("rank");
        String province = recommendConditionForm.getStr("province");
        String examType = recommendConditionForm.getStr("examType");
        String admitProbability = recommendConditionForm.getStr("admitProbability");

        QueryWrapper<WishRecommend> wishRecommendQueryWrapper = new QueryWrapper<WishRecommend>().eq("province",province).eq("exam_type",examType);
        //处理推荐
        switch (admitProbability){
            case "冲刺":
                wishRecommendQueryWrapper = wishRecommendQueryWrapper.between("lowest_rank",rank-1000.0,rank+1000.0);
                break;
            case "稳妥":
                wishRecommendQueryWrapper = wishRecommendQueryWrapper.between("lowest_rank",rank+1000.0,rank+3000.0);
                break;
            case "保底":
                wishRecommendQueryWrapper = wishRecommendQueryWrapper.between("lowest_rank",rank+3000.0,rank+5000.0);
                break;
            default:
                wishRecommendQueryWrapper = wishRecommendQueryWrapper.between("lowest_rank",rank-1000.0,rank+5000.0);
                break;
        }
        Page page = new Page(currentPage,10);
        IPage iPage = wishRecommendService.page(page,wishRecommendQueryWrapper.orderByAsc("lowest_rank"));


        if (iPage.getRecords().isEmpty()){
            return Result.success(201,"推荐学校列表为空",iPage);
        }else{
            List<College> collegeList = getCollege(iPage);
            return Result.success(MapUtil.builder().put("ipage", iPage).put("collegeList",collegeList).map());
        }
    }


    /**
     * create index index_college_name on college(college_name);
     * //根据大学collegeName 获取指定大学信息
     * @param iPage
     * @return
     */
    private List<College> getCollege(IPage iPage){

        List<College> collegeList = new ArrayList<College>();
        List<WishRecommend> iPageRecords = iPage.getRecords();
        for (int i = 0; i < iPageRecords.size(); i++) {
            College college = collegeService.getOne(new QueryWrapper<College>().eq("college_name", iPageRecords.get(i).getCollegeName()));
            if (college == null){
                college = new College();
                college.setAvatar("暂无信息");
                college.setCollegeType("暂无信息");
                college.setRegion("暂无信息");
                college.setStartType("暂无信息");
            }
            collegeList.add(college);
            //处理分数，排位
            String lowestScore = new DecimalFormat("0").format(iPageRecords.get(i).getLowestScore());
            String lowestRank = new DecimalFormat("0").format(iPageRecords.get(i).getLowestRank());
            iPageRecords.get(i).setLowestScore(Double.valueOf(lowestScore));
            iPageRecords.get(i).setLowestRank(Double.valueOf(lowestRank));
        }
        return collegeList;
    }
}
